
//
//  CollectionView.m
//  afn
//
//  Created by mac on 2018/5/10.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "CollectionView.h"
#import "CollectionTableViewCell.h"
@implementation CollectionView

-(void)creatView{
    self.backgroundColor=_LightGrey;
    UIView *topView=[UIView new];
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(45);
    }];
    topView.backgroundColor=[UIColor whiteColor];
    
    _shopBtn=[UILabel new];
    [topView addSubview:_shopBtn];
    [_shopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(topView.mas_centerX).mas_equalTo(-30);;
    }];
    _shopBtn.text=@"店铺";
    _shopBtn.font=[UIFont fontWithName:@"Helvetica" size:15];
    _shopBtn.userInteractionEnabled=YES;
    _shopBtn.tag=1;
    [_shopBtn layoutIfNeeded];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectZhuangTai:)];
    [_shopBtn addGestureRecognizer:tapGesture];
    
    _shopLabel=[UIView new];
    [topView addSubview:_shopLabel];
    [_shopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-2);
        make.centerX.mas_equalTo(self->_shopBtn);
        make.width.mas_equalTo(self->_shopBtn.frame.size.width+20);
        make.height.mas_equalTo(3);
    }];
   
    
    _serviceBtn=[UILabel new];
    [topView addSubview:_serviceBtn];
    [_serviceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(topView.mas_centerX).mas_equalTo(30);;
    }];
    _serviceBtn.text=@"服务";
    _serviceBtn.font=[UIFont fontWithName:@"Helvetica" size:15];
    _serviceBtn.userInteractionEnabled=YES;
    _serviceBtn.tag=2;
    [_serviceBtn layoutIfNeeded];
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectZhuangTai:)];
    [_serviceBtn addGestureRecognizer:tapGesture2];
    
    
    _serviceLabel=[UIView new];
    [topView addSubview:_serviceLabel];
    [_serviceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-2);
        make.centerX.mas_equalTo(self->_serviceBtn);
        make.width.mas_equalTo(self->_serviceBtn.frame.size.width+20);
        make.height.mas_equalTo(3);
    }];
    
    _shopLabel.backgroundColor=_backgroundColor;
    _serviceLabel.backgroundColor=_backgroundColor;
    
    _scrollView=[UIScrollView new];
    [self addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topView.mas_bottom).mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    _scrollView.scrollsToTop = NO;
    _scrollView.contentSize = CGSizeMake(_mainW*2, 0);
    _scrollView.showsVerticalScrollIndicator = FALSE;
    _scrollView.showsHorizontalScrollIndicator = FALSE;
    _scrollView.pagingEnabled=YES;
    _scrollView.bounces = NO;
    _scrollView.delegate=self;
    _scrollView.tag=10;
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.viewController.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
    
    _shopTabelView=[BaseTableView new];
    [_scrollView addSubview:_shopTabelView];
    [_shopTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(_mainW);
        make.height.mas_equalTo(_mainH-NAV_HEIGHT-45);
    }];
    _shopTabelView.backgroundColor=_LightGrey;
    _shopTabelView.tag=1;
    _shopTabelView.delegate=self;
    _shopTabelView.dataSource=self;
    _shopTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self->_tableArr1 removeAllObjects];
        [self LoginDic1];
    }];
    
    _serviceTableView=[BaseTableView new];
    [_scrollView addSubview:_serviceTableView];
    [_serviceTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(_mainW);
        make.width.mas_equalTo(_mainW);
        make.height.mas_equalTo(_mainH-NAV_HEIGHT-45);
    }];
    _serviceTableView.tag=2;
    _serviceTableView.backgroundColor=_LightGrey;
    _serviceTableView.delegate=self;
    _serviceTableView.dataSource=self;
    _serviceTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self->_tableArr2 removeAllObjects];
        [self LoginDic1];
    }];
    
    
    if (_abc==1) {
        _abc=1;
        
        _scrollView.contentOffset=CGPointMake(0, 0);
        _shopBtn.textColor=ColorWithRGB(49, 49, 49);
        _serviceBtn.textColor=ColorWithRGB(149, 149, 149);
        
        _shopLabel.hidden=NO;
        _serviceLabel.hidden=YES;
        
    }
    if (_abc==2) {
        _abc=2;
        _scrollView.contentOffset=CGPointMake(_mainW, 0);
        
        _shopBtn.textColor=ColorWithRGB(149, 149, 149);
        _serviceBtn.textColor=ColorWithRGB(49, 49, 49);
        
        _shopLabel.hidden=YES;
        _serviceLabel.hidden=NO;
    }
    
     [self LoginDic1];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    if (tableView.tag==1) {
        return _tableArr1.count;
    }else if (tableView.tag==2) {
        return _tableArr2.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionTableViewCell *cell=[[CollectionTableViewCell alloc] init];

    if (tableView.tag==1) {
        cell.model=[CollectionModel mj_objectWithKeyValues:_tableArr1[indexPath.row]];
        [cell creatView2];
    }
    if (tableView.tag==2) {
        cell.model=[CollectionModel mj_objectWithKeyValues:_tableArr2[indexPath.row]];
        [cell creatView3];
    }
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
    
}
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath

{
   
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_isSelect) {
        CollectionModel *model;
        if (tableView.tag==1) {
            model=[CollectionModel mj_objectWithKeyValues:_tableArr1[indexPath.row]];
           
        }
        if (tableView.tag==2) {
            model=[CollectionModel mj_objectWithKeyValues:_tableArr2[indexPath.row]];
            
        }
        [_selectArr addObject:model];
    }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_isSelect) {
        CollectionModel *model;
        if (tableView.tag==1) {
            model=[CollectionModel mj_objectWithKeyValues:_tableArr1[indexPath.row]];
            
        }
        if (tableView.tag==2) {
            model=[CollectionModel mj_objectWithKeyValues:_tableArr2[indexPath.row]];
        }
        [_selectArr removeObject:model];
    }
}




-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.tag==10) {
        
        CGPoint points=scrollView.contentOffset;
        int pages=points.x/_mainW;
        if (pages==0) {
            _abc=1;
            _shopBtn.textColor=ColorWithRGB(49, 49, 49);
            _serviceBtn.textColor=ColorWithRGB(149, 149, 149);
            
            _shopLabel.hidden=NO;
            _serviceLabel.hidden=YES;
            
        }
        if (pages==1) {
            _abc=2;
            _shopBtn.textColor=ColorWithRGB(149, 149, 149);
            _serviceBtn.textColor=ColorWithRGB(49, 49, 49);
            
            _shopLabel.hidden=YES;
            _serviceLabel.hidden=NO;
        }
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag==10) {
        [self LoginDic1];
        
    }
}

-(void)selectZhuangTai:(UITapGestureRecognizer *)tap{
    
    if (tap.view.tag==1) {
        _abc=1;
        
        _scrollView.contentOffset=CGPointMake(0, 0);
        _shopBtn.textColor=ColorWithRGB(49, 49, 49);
        _serviceBtn.textColor=ColorWithRGB(149, 149, 149);
        
        _shopLabel.hidden=NO;
        _serviceLabel.hidden=YES;
        
    }
    if (tap.view.tag==2) {
        _abc=2;
        _scrollView.contentOffset=CGPointMake(_mainW, 0);
        
        _shopBtn.textColor=ColorWithRGB(149, 149, 149);
        _serviceBtn.textColor=ColorWithRGB(49, 49, 49);
        
        _shopLabel.hidden=YES;
        _serviceLabel.hidden=NO;
    }

    [self LoginDic1];
}

-(void)LoginDic1{
    
    NSDictionary *dic;
    if (_abc==1) {
        if (_tableArr1.count!=0) {
            return;
        }
        dic=[NSDictionary dictionaryWithObjectsAndKeys:@"shop",@"type", nil];
    }
    else{
        if (_tableArr2.count!=0) {
            return;
        }
        dic=[NSDictionary dictionaryWithObjectsAndKeys:@"hairdresser",@"type", nil];
    }
    [gongju AFNPOSTDataUrl:@"v1/collection/collection-list" andDic:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
      
        if (self->_abc==1) {
            [self->_shopTabelView.mj_footer endRefreshing];
            [self->_shopTabelView.mj_header endRefreshing];
            [self->_tableArr1 addObjectsFromArray:responseObject];
            [self->_shopTabelView reloadData];
        }
        else{
            [self->_serviceTableView.mj_footer endRefreshing];
            [self->_serviceTableView.mj_header endRefreshing];
            [self->_tableArr2 addObjectsFromArray:responseObject];
            [self->_serviceTableView reloadData];
        }
        
    } anderrorBlock:nil];
}



@end
